const db = require("../db/index");
// 获取
exports.getCommunity = (req, res) => {
  const sql = `select * from ev_community where is_delete=0 order by id asc `;
  db.query(sql, (err, results) => {
    // 1. 执行 SQL 语句失败
    if (err) return res.cc(err);

    // 2. 执行 SQL 语句成功
    res.send({
      status: 0,
      message: "获取帖子信息成功！",
      data: results,
      total: results.length,
    });
  });
};

// 审核
exports.updateState = (req, res) => {
  const sql = `update ev_community set state =1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("审核通过失败!");
    res.send({ status: 0, message: "审核通过成功" });
  });
};

// 删除
exports.delteCommunity = (req, res) => {
  const sql = `update ev_community set is_delete = 1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("删除帖子失败!");
    res.send({ status: 0, message: "删除帖子成功!" });
  });
};
